#!/usr/bin/python
# -*- coding: UTF-8 -*-

import requests
import bs4
#import MainUtil
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker
from config import DB_URI

resources_file_path = '/resources/airplane/flightNameList.ini'
scratch_url = 'http://www.variflight.com/sitemap.html?AE71649A58c77='

engine = create_engine(DB_URI)
Base = declarative_base(engine)  # SQLORM基类
session = sessionmaker(engine)()  # 构建session对象

class Flight(Base):
    __tablename__ = 'flight'  # 表名
    id = Column(Integer, primary_key=True, autoincrement=True)
    flight = Column(String(500))


def scratch_flight_number(scratch_url, old_flights):
    #new_flights = []
    data = requests.get(scratch_url).text
    soup = bs4.BeautifulSoup(data, "html.parser")
    a_flights = soup.find('div', class_='list').find_all('a', recursive=False)
    for flight in a_flights:
        fli = Flight(flight=flight.text)  # 创建一个student对象
        session.add(fli)  # 添加到session
        session.commit()  # 提交到数据库
    #print("Hello World")
    #return new_flights



# Base.metadata.create_all()
scratch_flight_number(scratch_url, scratch_flight_number)